package com.test.security.springsecuritydemo8.filter;

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.web.filter.OncePerRequestFilter;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @since 2023/1/3 下午5:17
 */
public class CsrfTokenLogger extends OncePerRequestFilter {
    private static final Logger LOGGER = LoggerFactory.getLogger(CsrfTokenLogger.class);

    /**
     * @param request
     * @param response
     * @param filterChain
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        final Object csrf = request.getAttribute("_csrf");
        CsrfToken token = (CsrfToken) csrf;
        LOGGER.info("CSRF token {}", token.getToken());
        filterChain.doFilter(request, response);
    }
}
